const ajax = require('../../tool/ajax');
const history = require('../../tool/history');
const screen = require('./screen');//截图

/**
 * 当事人补充材料-被动
 * @param pupConfig 框架基础配置信息
 * @param type 类型，0代表立案的补充材料，1代表庭审的补充材料-被动
 */
const start = async ({page, config}, type = 0) => {
  let result;
  if (type == 0) {
    result = await ajax.waitForResponse(page, 'queryAllFileList.do');
    result = result.data && result.data.fileList;
  } else {
    result = await ajax.waitForResponse(page, 'queryNoticeSupplementalMaterials.do');
    //页面地址的参数
    let urlParams = history.getUrlParams(page);
    result = result.data.noticeList.find((item) => item.noticeId === urlParams.noticeId).courtMaterialFileList;
  }

  //点击补充材料
  if (result && result.length > 0) {
    let inputs = await page.$$('span.ant-upload input');
    for (let i = 0; i < result.length; i++) {
      await inputs[i].uploadFile(config.common.imgPathUpload + 'girl.png');
      await ajax.waitForResponse(page, 'getFileInfo.do');
    }
  }

  await screen(page,'补充材料');

  /**
   * 点击确认提交
   */
  await page.click('button.submit');
  await page.waitFor(1000);
  await page.click('.ant-modal-footer button.tool-modal-confirm');
  //确认进入详情页
  await ajax.waitForResponse(page, 'getAllCaseInfo.do');
}

module.exports = start;